﻿using Kingdee.BOS.App;
using Kingdee.BOS.App.Data;
using Kingdee.BOS.Contracts;
using Kingdee.BOS.Core.Report;
using Kingdee.BOS.Util;
using Kingdee.K3.FIN.AR.App.Report;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DzInfo.Report.ND
{
    [Description("应收款汇总表服务端插件_纳德扩展")]

    public class ARSumReportServiceExt : ARSumReportService
    {
        private string[] temptables;
        public override void BuilderReportSqlAndTempTable(IRptParams filter, string tableName)
        {
            temptables = CommonFuncReport.CreateTemporaryTableNames(1, base.Context);
            string strTable = temptables[0];
            base.BuilderReportSqlAndTempTable(filter, strTable);
            StringBuilder sb = new StringBuilder();
            string strSql = @"/*dialect*/ 
            select t1.*, A1.F_ND_XSLX 
            into {0}  FROM {1} t1 
            left join (
               SELECT MAX(T4.FDATAVALUE) F_ND_XSLX,T0.FCUSTOMERID 
               FROM t_AR_receivable T0
               LEFT JOIN T_BAS_ASSISTANTDATAENTRY_L T4 ON T4.FENTRYID=T0.F_NDN_ASSISTANT1 AND T4.FLOCALEID=2052
               GROUP BY T0.FCUSTOMERID
            ) A1 ON A1.FCUSTOMERID=t1.fcustid
            ";
            sb.AppendFormat(strSql, tableName, strTable);
            DBUtils.Execute(this.Context, sb.ToString());
        }
        public override void CloseReport()
        {
            //删除临时表
            if (temptables != null && temptables.Count() >= 1)
            {
                ITemporaryTableService service = ServiceFactory.GetService<ITemporaryTableService>(base.Context);
                service.DropTable(base.Context, new HashSet<string>(temptables));
                base.CloseReport();
            }
        }
    }
}
